package ru.spb.osll.myfit.server.handler;

import ru.spb.osll.myfit.common.TextParsingTools;
import ru.spb.osll.myfit.server.parser.PageUtils;
import ru.spb.osll.myfit.util.FileHelper;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class PageNamesLoader {
  private FileHelper m_helper = new FileHelper();

  public List<String> loadNames(String finesseRoot){
    String fullPath = PageUtils.getFullPathToContent(finesseRoot, TextParsingTools.ADDRESS_BAR_SEPARATOR);
    File file =  m_helper.getFile(fullPath);

    List<String> names = new LinkedList<String>();
    names.addAll(getChildren(file.getParentFile(), "", true));

    return names;
  }

  private List<String> getChildren(File parent, String parentName, boolean isFirst){
    List<String> names = new LinkedList<String>();

    File files[] = m_helper.listFiles(parent);
    if(files == null)
      return names;

    String name;
    for(File file : files){
      if(m_helper.isDirectory(file)){
        name = parentName + (isFirst ? "" : TextParsingTools.ADDRESS_BAR_SEPARATOR) + file.getName();
        names.add(name);
        names.addAll(getChildren(file, name, false));
      }
    }

    return names;
  }
}
